'''
Created on Apr 22, 2013

@author: Wagner
'''

from Tkinter import *
import Tkinter
import CacheProxy, Server, time

class GUI(object):
   
    def __init__(self):
        self.started = False
        top = Tkinter.Tk()
                
        L1 = Label(top, text="Activate page Caching?")
        L1.pack( side = TOP)
        
        self.var = IntVar()
        R1 = Radiobutton(top, text="Active", variable = self.var, value=1)
        R1.pack( anchor = W, side = TOP )
        print self.var
        R2 = Radiobutton(top, text="Inactive", variable = self.var, value=2)
        R2.pack( anchor = W, side = TOP)
        
        L1 = Label(top, text="Max Cache Size (bytes)")
        L1.pack( side = TOP)
        self.txtMaxCacheSize = Entry(top, bd = 2)
        self.txtMaxCacheSize.insert(0, '1048576')
        self.txtMaxCacheSize.pack(side = TOP)
        
        L2 = Label(top, text="Cache Lifetime (s)")
        L2.pack( side =  TOP)
        self.txtCacheLifeTime = Entry(top, bd = 2)
        self.txtCacheLifeTime.insert(0, "600")
        self.txtCacheLifeTime.pack(side = TOP)
        
        L3 = Label(top, text="Unused Files Lifetime (s)")
        L3.pack( side =  TOP)
        self.txtUnusedFilesLifeTime = Entry(top, bd = 2)
        self.txtUnusedFilesLifeTime.pack(side = TOP)
        
        L4 = Label(top, text="Cache Lock Timeout (s)")
        L4.pack( side =  TOP)
        self.txtCacheLockTimeout = Entry(top, bd = 2)
        self.txtCacheLockTimeout.insert(0, '600')
        self.txtCacheLockTimeout.pack(side = TOP)
        
        self.txtCacheOccupied = StringVar()
        self.txtCacheOccupied.set("Cache usage : 0")
        L5 = Label(top, textvariable=self.txtCacheOccupied)
        L5.pack(side = BOTTOM)
        
        btnLaunch = Tkinter.Button(top, text = "Launch", command = self.start)
        btnLaunch.pack(side = BOTTOM)
        
        btnQuit = Tkinter.Button(top, text = "Shut Down", command = self.terminate)
        btnQuit.pack(side = BOTTOM)
           
        top.mainloop()
    def start(self):
        maxCacheSize = self.txtMaxCacheSize.get()
        cacheLifeTime = self.txtCacheLifeTime.get()
        unusedFilesLifeTime = self.txtUnusedFilesLifeTime.get()
        cacheLockTimeout = self.txtCacheLockTimeout.get()
        radioButtonStatus = self.var.get()
        if radioButtonStatus == 1:
            cacheActive = True
        else:
            cacheActive = False
        print radioButtonStatus
        
        
        #txtSize = d.getSize()
        #self.txtCacheOccupied.set("Cache usage : 0" + str(txtSize))
        if not self.started:            
            self.serv = Server.Server(self.txtCacheOccupied, cacheActive, maxCacheSize, int(cacheLifeTime), int(cacheLockTimeout))
            self.serv.start()
            self.started = True
        
        time.sleep(10)
        #print cacheSize
    def terminate(self):
        self.serv.stop()
        self.started = False
        
def main():
    g = GUI()
    maxCacheSize = g.txtMaxCacheSize.get()
    cacheLifeTime = g.txtCacheLifeTime.get()
    unusedFilesLifeTime = g.txtUnusedFilesLifeTime.get()
    cacheLockTimeout = g.txtCacheLockTimeout.get()
    cacheActive = False
    if g.var == 1:
        cacheActive = True
    else:
        cacheActive = False
        
  

if __name__ == "__main__":
    main()